//
//  VVUserAccountViewModel.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/30.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit

class VVUserAccountViewModel: NSObject {
    
    //单例的全局访问点
    static let sharedViewModel: VVUserAccountViewModel = VVUserAccountViewModel()
    
    //重写init构造方法
    private override init() {
        
        super.init()
        
        //获取本地归档的对象
        userAccount = VVUserAccount.loadUserAccount()
    }
    
    //viewModel引用模型
    var userAccount:VVUserAccount?
    
    //封装获取accessToken的逻辑
    var accessToken: String? {
        
        //获取过期时间对象 orderedDescending递减判断
        if userAccount?.expiresDate?.compare(Date()) == ComparisonResult.orderedDescending {
            
            //如果过期时间大于当前时间,表示没有过期
            return userAccount?.access_token
        }
        
        return nil
    }
    
    //封装是否登录的操作
    var islogin: Bool {
        
        return accessToken != nil
    }
    
    //通过code获取accesstoken
    func requestAccessToken(code: String, callBack:@escaping (Bool)->()) {
        
        VVNetworkTools.sharedTools.requestAccessToken(code: code) { (response, error) in
            
            if error != nil {
                
                print("网络请求异常:\(error!)")
                
                return
            }
            
            //print(response);
            guard let dic = response as? [String: Any] else {
                
                print("服务器端数据不对")
                
                return
            }
            
            //代码执行到此表示oc对象
            print(dic)
            
            let userAccount = VVUserAccount(dic: dic)
            
            //通过uid和accessToken获取用户数据
            //self.requestUserInfo(userAccount: userAccount)
            self.requestUserInfo(userAccount: userAccount, callBack: callBack)
            
        }
        
    }
    //通过accessToken和uid获取用户
    fileprivate func requestUserInfo(userAccount: VVUserAccount,callBack:@escaping (Bool)->()) {
        
        //获取令牌
        let accessToken = userAccount.access_token!
        
        //用户id
        let uid = userAccount.uid
        
        VVNetworkTools.sharedTools.requestUserInfo(accessToken: accessToken, uid: uid) { (response, error) in
            
            if error != nil {
                
                print("网络请求异常:\(error!)")
                
                return
            }
            
            
            guard let dic = response as? [String: Any] else {
                
                print("服务端数据格式不对")
                
                return
            }
            
            //给对象里面的name和头像赋值
            let name = dic["name"] as? String
            
            let avatar_large = dic["avatar_large"] as? String
            
            userAccount.name = name
            
            userAccount.avatar_large = avatar_large
            
            //print(userAccount.name, userAccount.avatar_large)
            
            
            //保存用户对象
            userAccount.saveUserAccount()
            
            //给属性赋值
            self.userAccount = userAccount
            
            //获取到用户信息并且归档完成表示登录成功
            callBack(true)
        }
        
        
        
    }
    
}

